class Admin::ItemStylesController < AdminController
  before_filter :find_item_style, :only => [:show, :edit, :update, :destroy]
  
  def index
    @item_styles = ItemStyle.valid.filter(params).paginate :page => params[:page], :per_page => 30
  end
  
  def show
  end
  
  def new
    @item_style = ItemStyle.new
  end
  
  def create
    @item_style = ItemStyle.new(params[:item_style])
    if @item_style.save
      flash[:notice] = "Successfully created item style."
      redirect_to params[:save_and_new].blank? ? edit_admin_item_style_path(@item_style) : new_admin_item_style_path
    else
      render 'new'
    end
  end
  
  def edit
  end
  
  def update
    if @item_style.update_attributes(params[:item_style])
      flash[:notice] = "Successfully updated item style."
      redirect_to params[:save_and_new].blank? ? edit_admin_item_style_path(@item_style) : new_admin_item_style_path
    else
      render 'edit'
    end
  end
  
  def destroy
    @item_style.del
    flash[:notice] = "Successfully destroyed item style."
    redirect_to admin_item_styles_url
  end
  
  def update_status
    if params[:ids].blank? or params[:ids].all?{|id| id.blank? }
      flash[:error] = "操作失败."
    else
      status = params[:status].blank? ? 0 : params[:status].to_i
      ItemStyle.update_all("status = #{status}", "id IN (#{params[:ids].join(',')})")
      flash[:notice] = "操作成功."
    end
    redirect_to admin_item_styles_url
  end
  
  private
  
  def find_item_style
    @item_style = ItemStyle.find(params[:id])
  end
end
